package com.kh.statistics.video.core.login;

import com.kh.statistics.video.constant.PlatformConstant;
import com.kh.statistics.video.core.login.entity.DongCheDiHealthCheckResponse;
import com.kh.statistics.video.core.login.entity.DouyinHealthCheckResponse;
import com.kh.statistics.video.entity.StatisticsMeta;
import com.kh.statistics.video.entity.VideoAccountsHealth;
import com.kh.statistics.video.entity.VideoAccountsLogin;
import com.kh.statistics.video.helper.CookieHelper;
import com.kh.statistics.video.helper.HttpServletHelper;
import com.kh.statistics.video.helper.StatisticsHttpHelper;
import com.kh.statistics.video.helper.VideoAccountsHealthHelper;
import com.kh.statistics.video.utils.SerializationUtils;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * @BelongsProject: statistics
 * @BelongsPackage: com.kh.statistics.video.core.login
 * @Author: Hong.Kong
 * @CreateTime: 2024-05-13  14:06
 * @Description: TODO
 * @Version: 1.0
 */
@Component
@Slf4j
public class DongCheDiAccountsLogin implements AccountsLogin {

    @Override
    public String support() {
        return PlatformConstant.DONG_CHE_DI;
    }

    @Override
    public Boolean login(VideoAccountsLogin videoAccountsLogin, WebDriver driver) throws IOException, InterruptedException {
        boolean flag = true;
        String content = null;
        while (flag || null == content) {
            driver.get("https://mp.dcdapp.com/login/?redirect_url=JTJGJTNGbGlua19zb3VyY2UlM0RwYw");
            Thread.sleep(2000);
            WebElement element = driver.findElement(By.className("web-login-confirm-info__checkbox"));
            element.click();
            List<WebElement> elements = driver.findElements(By.className("web-login-other-login-method__list__item"));
            elements.get(1).click();
            Thread.sleep(1000);
            flag = false;
            content = driver.getPageSource();
        }
        HttpServletHelper.writer(content);
        CookieHelper.getCookies(driver, "懂车帝");
        CookieConvertMyCookie.setCookies(driver, videoAccountsLogin);
        WebDriver.Options manage = driver.manage();
        videoAccountsLogin.setCookieSet(CookieConvertMyCookie.CookiesToMyCookies(manage.getCookies()));
        return true;
    }

    @Override
    public VideoAccountsHealth creatorHealthChecks(VideoAccountsLogin videoAccountsLogin) {
        // 判断是否是账户是否已经存在
        StatisticsMeta statisticsMeta = new StatisticsMeta();
        statisticsMeta.setRequestType("get");
        statisticsMeta.setUrl("https://mp.dcdapp.com/motor/mp_index/api/notify/count");
        HashMap<String, String> map = new HashMap<>();
        map.put("cookie", videoAccountsLogin.getCookies());
        statisticsMeta.setHeaders(map);
        String body = StatisticsHttpHelper.request(statisticsMeta);
        VideoAccountsHealth videoAccountsHealth = VideoAccountsHealthHelper.getVideoAccountsHealth(videoAccountsLogin);
        videoAccountsHealth.setPlatformName("懂车帝");
        DongCheDiHealthCheckResponse response = SerializationUtils.deserialize(body, DongCheDiHealthCheckResponse.class);
        if (response.getStatus() == 0) {
            videoAccountsHealth.setStatus(1);
            log.info("懂车帝健康检查正常:account:{},response:{}", videoAccountsHealth, response);
        } else {
            log.info("懂车帝健康检查异常:account:{},response:{}", videoAccountsHealth, response);
        }
        return videoAccountsHealth;
    }

    @Override
    public VideoAccountsHealth webHealthChecks(VideoAccountsLogin videoAccountsLogin) {
        return null;
    }
}
